package zmwxapplet

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/helper/resphelper/placeresp"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)

// HandleCreateInviteUserRegister 邀请码用户注册
func HandleCreateInviteUserRegister(ctx *gin.Context) {
	param := &request.CreateInviteUserRegisterReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	memberId, _ := helper.GetUserIdFromCtx(ctx)
	wifiUserInfo := dao.NewUserDao().GetWifiUserInfoByMemberId(memberId).Unwrap().(*users.WifiUserDb)

	userInfo := dao.NewUserDao().GetUserInfoByMemberId(memberId).Unwrap().(*users.UserModel)
	insertId := userInfo.ID
	if wifiUserInfo.ID > 0 && userInfo.ID > 0 {
		Response(ctx)("用户己存在", CodeError, false)(OK)
		return
	}

	inviteInfo := dao.NewUserDao().GetInviteCodeById(param.InviteId).Unwrap().(*users.InviteConfigModel)
	if inviteInfo.ID <= 0 {
		Response(ctx)("无效邀请码", CodeError, false)(OK)
		return
	}

	dbInfo := &request.AdminUserInsertReq{
		ParentId:         inviteInfo.UserId,
		UserName:         param.UserName,
		Name:             param.UserName,
		Phone:            param.Phone,
		ProfitProportion: inviteInfo.BelowProportion,
		IsAdmin:          inviteInfo.IsAdmin,
		Password:         "123456",
		Status:           1,
		Platform:         "wifi", // 新增加入可登录平台
	}
	if insertId > 0 {
		// 更新平台
		var platform string
		if !helper.InSlice(strings.Split(userInfo.Platform, ","), "wifi") {
			if userInfo.Platform == "" {
				platform = utils.Join("wifi")
			} else {
				platform = utils.Join(userInfo.Platform, ",", "wifi")
			}
			dao.NewUserDao().UpdateUserPlatform(platform, userInfo.ID)
		}
	} else if inviteInfo.IsAdmin == helper.TypeNumIsAdminBd {
		data := dao.NewUserDao().InsertBdUser(dbInfo)
		insertId = data.Unwrap().(int)
		fmt.Println(data.Err, insertId)
	} else if inviteInfo.IsAdmin == helper.TypeNumIsAdminMerchant {
		data := dao.NewUserDao().InsertPlaceUser(dbInfo)
		insertId = data.Unwrap().(int)
		fmt.Println(data.Err, insertId)
	} else if inviteInfo.IsAdmin == helper.TypeNumIsAdminResource {
		data := dao.NewUserDao().InsertAgentUser(dbInfo)
		insertId = data.Unwrap().(int)
		fmt.Println(data.Err, insertId)
	}
	if insertId <= 0 {
		Response(ctx)("注册失败-1", CodeError, false)(OK)
		return
	}

	dbRecord := &users.InviteRecordDb{
		ParentId: inviteInfo.UserId,
		UserId:   insertId,
		InviteId: inviteInfo.ID,
		Name:     param.UserName,
		Phone:    param.Phone,
	}
	if InviteRecordData := dao.NewUserDao().CreateInviteRecord(dbRecord); InviteRecordData.Err != nil {
		Response(ctx)("注册失败-2", CodeError, false)(OK)
		return
	}

	dbWifiUser := &users.WifiUserDb{
		ID:               wifiUserInfo.ID,
		UserId:           insertId,
		MemberId:         memberId,
		ParentId:         inviteInfo.UserId,
		ProfitProportion: inviteInfo.BelowProportion,
		IsAdmin:          inviteInfo.IsAdmin,
		IsDivinds:        1, //默认参与分成
	}
	if wUserData := dao.NewUserDao().CreateWifiUser(dbWifiUser); wUserData.Err != nil {
		Response(ctx)("注册失败-3", CodeError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleCreateInviteCode 创建邀请码
func HandleCreateInviteCode(ctx *gin.Context) {
	param := &users.InviteConfigDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if param.IsAdmin != 3 && param.IsAdmin != 9 && param.IsAdmin != 8 {
		Response(ctx)("", CodeSuccess, false)(OK)
		return
	}

	memberId, _ := helper.GetUserIdFromCtx(ctx)
	userInfo := dao.NewUserDao().GetUserInfoByMemberId(memberId).Unwrap().(*users.UserModel)
	param.UserId = userInfo.ID
	param.UserProportion = userInfo.ProfitProportion

	if userInfo.ID <= 0 {
		Response(ctx)("用户错误", CodeError, false)(OK)
		return
	}
	if userInfo.ProfitProportion < param.BelowProportion {
		Response(ctx)("分成大于上级", CodeError, false)(OK)
		return
	}

	data := dao.NewUserDao().CreateInviteCode(param).Unwrap().(*users.InviteConfigDb)
	if data.ID == 0 {
		Response(ctx)("创建失败", CodeError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, data.ID)(OK)
}

// HandleGetPlaceList 查询用户网点
func HandleGetPlaceList(ctx *gin.Context) {
	param := &request.GetStewardPlaceListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	memberId, _ := helper.GetUserIdFromCtx(ctx)
	userInfo := dao.NewUserDao().GetUserInfoByMemberId(memberId).Unwrap().(*users.UserModel)
	param.LoginUserId = userInfo.ID
	param.IsAdmin = userInfo.IsAdmin
	param.ParentId = userInfo.ParentID

	if userInfo.ID <= 0 {
		Response(ctx)("", CodeSuccess, ResponseList{
			PageNo:     param.PageNo,
			PageSize:   param.PageSize,
			TotalCount: 0,
			TotalPage:  0,
			Data:       nil,
		})(OK)
		return
	}

	data := dao.NewPlaceDao().GetStewardSearchPlaceList(param)
	lists := data.Unwrap().([]*request.GetStewardSearchPlaceListRes)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       lists,
	})(OK)
}

func HandleGetWifiPlaceList(ctx *gin.Context) {
	param := &request.GetWifiPlaceListReq{}
	dingo.ErrorIf(ctx.ShouldBind(param)).Unwrap()

	memberId, _ := helper.GetUserIdFromCtx(ctx)
	userInfo := dao.NewUserDao().GetUserInfoByMemberId(memberId).Unwrap().(*users.UserModel)
	param.LoginUserId = userInfo.ID
	param.ParentId = userInfo.ParentID

	if userInfo.ID <= 0 {
		Response(ctx)("", CodeSuccess, ResponseList{
			PageNo:     param.PageNo,
			PageSize:   param.PageSize,
			TotalCount: 0,
			TotalPage:  0,
			Data:       nil,
		})(OK)
		return
	}

	data := dao.NewWifiDao().GetWifiPlaceList(param)
	lists := data.Unwrap().([]*placeresp.WifiPlaceListResp)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       lists,
	})(OK)
}
